import React from 'react';
import { Navigate, Route, createRoutesFromElements } from 'react-router-dom';
import Dashboard from './Dashboard';
import UserManagement from './UserManagement';
import UserDetails from './UserDetails';
import ContentManagement from './ContentManagement';
import VersionManagement from './VersionManagement';
import PermissionDenied from './PermissionDenied';
import FaceAnalysisConfig from './content/FaceAnalysisConfig';
import PalmAnalysisConfig from './content/PalmAnalysisConfig';
import BaziAnalysisConfig from './content/BaziAnalysisConfig';

// 权限路由组件 - 检查用户是否有管理员权限
const PermissionRoute = ({ children }) => {
  // 在实际应用中，这里应该从认证状态中获取用户权限
  // 这里使用模拟数据
  const isAdmin = true; // 假设当前用户是管理员
  
  if (!isAdmin) {
    return <Navigate to="/admin/permission-denied" replace />;
  }
  
  return children;
};

// 管理员路由配置
const adminRoutes = createRoutesFromElements(
  <Route>
    {/* 使用权限路由包装所有需要管理员权限的路由 */}
    <Route path="/dashboard" element={
      <PermissionRoute>
        <Dashboard />
      </PermissionRoute>
    } />
    
    <Route path="/content" element={
      <PermissionRoute>
        <ContentManagement />
      </PermissionRoute>
    } />
    
    <Route path="/versions" element={
      <PermissionRoute>
        <VersionManagement />
      </PermissionRoute>
    } />
    
    <Route path="/users" element={
      <PermissionRoute>
        <UserManagement />
      </PermissionRoute>
    } />
    
    <Route path="/users/:userId" element={
      <PermissionRoute>
        <UserDetails />
      </PermissionRoute>
    } />
    
    {/* 内容管理模块路由 */}
    <Route path="/content/face-analysis" element={
      <PermissionRoute>
        <FaceAnalysisConfig />
      </PermissionRoute>
    } />
    
    <Route path="/content/palm-analysis" element={
      <PermissionRoute>
        <PalmAnalysisConfig />
      </PermissionRoute>
    } />
    
    <Route path="/content/bazi-analysis" element={
      <PermissionRoute>
        <BaziAnalysisConfig />
      </PermissionRoute>
    } />
    
    {/* 权限拒绝页面，不需要权限检查 */}
    <Route path="/permission-denied" element={<PermissionDenied />} />
    
    {/* 默认重定向到仪表盘 */}
    <Route path="*" element={<Navigate to="/admin/dashboard" replace />} />
  </Route>
);

export default adminRoutes;